package com.ncf.jucai.mybatis.generator;

import com.ncf.jucai.mybatis.annotation.PagedEntityExample;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

public class PaginationPlugin extends PluginAdapter {

	@Override
	public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
			IntrospectedTable introspectedTable) {
		addLimit(topLevelClass, introspectedTable, "offset");
		addLimit(topLevelClass, introspectedTable, "size");
		addLimit(topLevelClass, introspectedTable, "totalCount");
        topLevelClass.addImportedType(PagedEntityExample.class.getName());
        topLevelClass.addAnnotation("@" + PagedEntityExample.class.getSimpleName());
		return super.modelExampleClassGenerated(topLevelClass,introspectedTable);
	}

	@Override
	public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
			XmlElement element, IntrospectedTable introspectedTable) {
		XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$  
		isNotNullElement.addAttribute(new Attribute("test", "offset != null and offset>=0")); //$NON-NLS-1$ //$NON-NLS-2$
		isNotNullElement.addElement(new TextElement("limit #{offset} , #{size}"));
		element.addElement(isNotNullElement);
		return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,
				introspectedTable);
	}

	@Override
	public boolean providerSelectByExampleWithoutBLOBsMethodGenerated(
            Method method,
            TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {
        List<String> bodyLines = method.getBodyLines();
        List<String> fixedLines = bodyLines.subList(0, bodyLines.size() - 1);
        fixedLines.add("String sql = SQL();");
        fixedLines.add("if (example.getSize() != null) {");
        fixedLines.add("sql += \" LIMIT #{size}\";");
        fixedLines.add("if (example.getOffset() != null) {");
        fixedLines.add("sql += \" OFFSET #{offset}\";");
        fixedLines.add("}");
        fixedLines.add("}");
        fixedLines.add("return sql;");
        try {
            java.lang.reflect.Field field = method.getClass().getDeclaredField("bodyLines");
            field.setAccessible(true);
            field.set(method, fixedLines);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.providerSelectByExampleWithoutBLOBsMethodGenerated(method, topLevelClass, introspectedTable);
	}

	private void addLimit(TopLevelClass topLevelClass,
						  IntrospectedTable introspectedTable, String name) {
		CommentGenerator commentGenerator = context.getCommentGenerator();
		Field field = new Field();
		field.setVisibility(JavaVisibility.PROTECTED);
		field.setType(PrimitiveTypeWrapper.getIntegerInstance());
		field.setName(name);
		commentGenerator.addFieldComment(field, introspectedTable);
		topLevelClass.addField(field);
		char c = name.charAt(0);
		String camel = Character.toUpperCase(c) + name.substring(1);
		Method method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setName("set" + camel);
		method.addParameter(new Parameter(PrimitiveTypeWrapper.getIntegerInstance(), name));
		method.addBodyLine("this." + name + "=" + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		topLevelClass.addMethod(method);
		method = new Method();
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setReturnType(PrimitiveTypeWrapper.getIntegerInstance());
		method.setName("get" + camel);
		method.addBodyLine("return " + name + ";");
		commentGenerator.addGeneralMethodComment(method, introspectedTable);
		topLevelClass.addMethod(method);
	}

	public boolean validate(List<String> warnings) {
		return true;
	}

}